import 'package:edu_assistant/page_data.dart';
import 'package:flutter/material.dart';
import 'package:edu_assistant/pages/file_page.dart';
import 'package:edu_assistant/pages/media_page.dart';
import 'package:edu_assistant/pages/filterable_list_page.dart';
import 'package:edu_assistant/pages/upload_file_page.dart';
import 'package:edu_assistant/pages/test_layout.dart';
import 'package:edu_assistant/pages/friends_circle.dart';
import 'package:edu_assistant/pages/test2.dart';
import 'package:edu_assistant/pages/gzx_dropdown_menu_page.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

class Home2Page extends StatefulWidget {
  @override
  _Home2PageState createState() => _Home2PageState();
}

class _Home2PageState extends State<Home2Page> {
  var childKey = GlobalKey<FilePageState>();
  int _currentIndex = 0;
  late List<Widget> _pages;
  late PageController _pageController;

  DateTime? _currentBackPressTime;
  DateTime? _lastTapTime;

  late FilePage filePage;
  late UploadFilePage uploadFilePage;
  late TestPage testPage;
  late FriendsCirclePage friendsCirclePage;
  late GZXDropDownMenuTestPage gzxDropDownMenuTestPage;
  late ToutiaoMyPage toutiaoMyPage;
  late MediaPage mediaPage;
  late FilterableListPage filterableListPage;

  @override
  void initState() {
    super.initState();
    filePage = FilePage(key: childKey);
    uploadFilePage = UploadFilePage();
    testPage = TestPage();
    friendsCirclePage = FriendsCirclePage();
    toutiaoMyPage = ToutiaoMyPage();
    gzxDropDownMenuTestPage = GZXDropDownMenuTestPage();
    mediaPage = MediaPage();
    filterableListPage = FilterableListPage();
    _pages = <Widget>[
      filePage,
      uploadFilePage,
      toutiaoMyPage,
      mediaPage,
      filterableListPage
    ];
    _pageController = PageController(
      initialPage: 0,
      keepPage: true,
    );
    _pageController.addListener(() {
      int page = _pageController.page?.round() ?? 0;
      setState(() {
        _currentIndex = page;
      });
    });
  }

  //双击返回键退出
  bool _doubleExit() {
    if (_currentBackPressTime == null ||
        DateTime.now().difference(_currentBackPressTime!) >
            const Duration(seconds: 2)) {
      //两次点击间隔超过1秒则重新计时
      _currentBackPressTime = DateTime.now();
      Fluttertoast.showToast(msg: "再按一次退出");
      return false;
    } else {
      // 退出请求有效
      _currentBackPressTime = null;
      return true;
    }
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (bool didPop, Object? result) async {
        if (didPop) {
          return;
        }
        if (_doubleExit()) {
          // 系统级别导航栈 退出程序
          SystemNavigator.pop();
        }
      },
      child: ChangeNotifierProvider(
        create: (_) => PageData(),
        child: _getBody(),
      ),
    );
  }

  Widget _getBody() {
    return Scaffold(
        body: PageView(
          controller: _pageController,
          children: _pages,
        ),
        bottomNavigationBar: Theme(
            data: ThemeData(
              highlightColor: Colors.transparent,
              splashColor: Colors.transparent,
            ),
            child: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              currentIndex: _currentIndex,
              onTap: (index) {
                final now = DateTime.now();
                if (_lastTapTime == null ||
                    now.difference(_lastTapTime!) >
                        Duration(milliseconds: 300)) {
                } else {
                  print('BottomNavigationBar double tapped on item $index');
                  if (index == 0) {
                    childKey.currentState?.refreash();
                  }
                }
                _lastTapTime = now;
                if (index == _currentIndex) {
                  print('BottomNavigationBar 点击当前页面 $index');
                  return;
                }
                _pageController.animateToPage(
                  index,
                  duration: Duration(milliseconds: 300),
                  curve: Curves.ease,
                );
                setState(() {
                  _currentIndex = index;
                });
              },
              unselectedFontSize: 10, // 未选中字体大小
              selectedFontSize: 10, // 选中字体大小
              unselectedItemColor: Colors.blueGrey,
              backgroundColor: Colors.white,
              showSelectedLabels: true,
              selectedItemColor: Colors.amber[800],
              items: const [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: '文件',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.sd_card),
                  label: '宏宇',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.dashboard),
                  label: '测试布局',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.shopping_cart),
                  label: '媒体',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.person),
                  label: '我的',
                ),
              ],
            )));
  }
}

class InitPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(""),
    );
  }
}
